package com.project.service;

import com.alibaba.druid.util.StringUtils;
import com.project.common.utils.PublicUtils;
import com.project.dao.UserRoleMapper;
import com.project.entity.UserRole;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/25 19:25
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class UserRoleServiceImpl extends BaseService implements UserRoleService{

    @Autowired
    private  UserRoleMapper userRoleMapper;

    @Override
    public int add(UserRole userRole) {
        return userRoleMapper.insertSelective(userRole);
    }

    @Override
    public int addBatch(UserRole userRole) {
        int roleid = userRole.getRoleid();

        //先全部删除角色当前所授权的用户，然后再插入
        removeBatchByRoleId(roleid);

        String userIds = userRole.getUserIds();
        if (!StringUtils.isEmpty(userIds)) {
            List<Integer> ids = PublicUtils.getIntIdList(userIds, ",");
            for (Integer userId : ids) {
                UserRole userRole1 = new UserRole();
                userRole1.setRoleid(roleid);
                userRole1.setUserid(userId);
                add(userRole1);
            }
        }

        return 1;
    }
    @Override
    public int removeBatchByRoleId(int roleid) {

        return userRoleMapper.deleteByRoleId(roleid);
    }

    @Override
    public List<UserRole> findByCondition(UserRole userRole) {
        return userRoleMapper.selectByCondition(userRole);
    }

    @Override
    public List<UserRole> findByRoleId(int roleid) {
        return userRoleMapper.selectByRoleId(roleid);
    }

    @Override
    public List<UserRole> findResourcesByUserId(Integer userId) {

        return userRoleMapper.selectResourcesByUserId(userId);
    }

    @Override
    public  List<UserRole> findBatchByUserId(Integer userid) {
        return userRoleMapper.selectBatchByUserId(userid);
    }
}
